package com.getpebble.android.main.activity;

import android.app.Fragment;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.connection.ConnectionManagerFragment;
import com.getpebble.android.core.PblBaseActivity;
import com.getpebble.android.framework.developer.PblDeviceObserver.ConnectedDeviceChangeListener;
import com.getpebble.android.main.fragment.FragmentMetaData.FragmentType;
import com.getpebble.android.main.sections.mypebble.fragment.MyPebbleFragment;

public abstract class TapToConnectActivity extends PblBaseActivity {
    private OnClickListener mConnStatusClickListener = new OnClickListener() {
        public void onClick(View v) {
            if (TapToConnectActivity.this instanceof MainActivity) {
                TapToConnectActivity.this.switchFragmentByType(FragmentType.CONNECTION_MANAGER, null);
                return;
            }
            Intent intent = new Intent(TapToConnectActivity.this, MainActivity.class);
            intent.addFlags(335544320);
            intent.putExtra("show_cmf", true);
            TapToConnectActivity.this.startActivity(intent);
        }
    };
    private TextView mConnStatusFooter;
    private final ConnectedDeviceChangeListener mDeviceChangeListener = new ConnectedDeviceChangeListener() {
        public void notifyConnectedDeviceChanged() {
            TapToConnectActivity.this.mHandler.post(new Runnable() {
                public void run() {
                    TapToConnectActivity.this.setTapToConnectVisibility();
                }
            });
        }
    };
    private Handler mHandler = new Handler(Looper.getMainLooper());

    protected void setTapToConnectVisibility() {
        boolean show = false;
        Fragment fragment = getCurrentFragment();
        if ((fragment == null || !fragment.getClass().equals(ConnectionManagerFragment.class)) && PebbleApplication.getConnectedDeviceRecord() == null) {
            show = true;
        }
        if (this.mConnStatusFooter != null) {
            if (show) {
                this.mConnStatusFooter.setVisibility(0);
            } else {
                this.mConnStatusFooter.setVisibility(8);
            }
        }
        Fragment frag = getCurrentFragment();
        if (frag instanceof MyPebbleFragment) {
            ((MyPebbleFragment) frag).redraw();
        }
    }

    public void onResume() {
        super.onResume();
        if (this.mConnStatusFooter == null) {
            this.mConnStatusFooter = (TextView) findViewById(R.id.tap_to_connect_bottom_bar);
            this.mConnStatusFooter.setOnClickListener(this.mConnStatusClickListener);
        }
        setTapToConnectVisibility();
        PebbleApplication.addConnectedDeviceChangeListener(this.mDeviceChangeListener);
    }

    public void onPause() {
        super.onPause();
        PebbleApplication.removeConnectedDeviceChangeListener(this.mDeviceChangeListener);
    }
}
